package com.lc.user.express.ordering;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.UsedRouteAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetMyAddr;
import com.lc.user.express.model.UsedRouteModel;
import com.zcx.helper.http.AsyCallBack;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/7. 选择常用路线
 */
public class SelectUsedRouteActivity extends BaseSecondActivity {
    public static OnRefreshView onRefreshView = null;
    private ListView lv;
    private UsedRouteAdapter addrAdapter = null;
    private List<UsedRouteModel> list = new ArrayList<>();
    private LinearLayout ll_empty;//空白显示
    private RelativeLayout rl_add;//添加按钮
    private TextView tv_footer;

    private GetMyAddr getMyAddr = new GetMyAddr(AppContext.myPreferences.getUid(), new AsyCallBack<GetMyAddr.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetMyAddr.Info info) throws Exception {

            tv_footer.setText("不使用常用路线");
            list.clear();
            list.addAll(info.dataList);

            if (list.size() > 0) {
                setRightButtonVisibility(View.VISIBLE);
                setRightButtonText("管理", 100);
            } else {
                setRightButtonVisibility(View.GONE);
            }
            isSelcted();
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);
            ll_empty.setVisibility(View.VISIBLE);
            lv.setEmptyView(ll_empty);
            addrAdapter.updateListView(list);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_used_route);
        setTitle("选择常用路线");
        initView();
        getRoute();

    }

    private void initView() {
        rl_add = (RelativeLayout) findViewById(R.id.rl_add);
        rl_add.setOnClickListener(this);

        ll_empty = (LinearLayout) findViewById(R.id.ll_empty);
        lv = (ListView) findViewById(R.id.lv);

        View v = LayoutInflater.from(cnt).inflate(R.layout.footer_select_used_addr_list, null);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) v);
        tv_footer = (TextView) v.findViewById(R.id.tv_dic);

        lv.addFooterView(v);
        tv_footer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (OrderInfoCompleteActivity.onAddrListChoosed != null) {
                    OrderInfoCompleteActivity.onAddrListChoosed.onChoosed("-99");
                }
                finish();
            }
        });

        addrAdapter = new UsedRouteAdapter(cnt, list);
        lv.setAdapter(addrAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (OrderInfoCompleteActivity.onAddrListChoosed != null) {
                    OrderInfoCompleteActivity.onAddrListChoosed.onChoosed(list.get(position).getRouteId());

                   AppContext.myPreferences.saveRouteId(list.get(position).getRouteId());


                }
                finish();
            }
        });
        onRefreshView = new OnRefreshView() {
            @Override
            public void onRefresh() {
                getRoute();
            }
        };

    }

    private void getRoute() {
        getMyAddr.execute(cnt);
    }

    private void isSelcted() {
        for (UsedRouteModel model : list) {
            if (getIntent().getStringExtra("routeid").equals(model.getRouteId())) {
                model.setIsSelect(true);
                return;
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.head_right:
                Bundle bundle = new Bundle();
                bundle.putSerializable("routelist", (Serializable) list);
                show(ManageUsedRouteActivity.class, bundle);
                break;
            case R.id.rl_add:
                show(AddUsedRouteActivity.class, AddUsedRouteActivity.TYPE_ADD);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onRefreshView = null;
    }

    public interface OnRefreshView {
        void onRefresh();
    }
}
